package com.ljx.offer;

/**
 * @author 李捷禧
 * Date: 2023/3/25
 * ClassName: C_20
 */

public class C_20 {
    /**
     * 常规解法
     * @param s
     * @return
     */
    public boolean isNumber(String s) {
        if(s == null ||s.length() == 0) return false;
        char[] arr = s.trim().toCharArray();
        //表示数值
        boolean isNum = false;
        //表示小数点
        boolean isDot = false;
        //表示E/e
        boolean isEe = false;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] >= '0' && arr[i] <= '9') isNum = true;
            else if(arr[i] == '.') {
                //前面的数如果有e/E/’.'就直接返回false
                if(isEe || isDot) return false;
                isDot = true;
            }else if(arr[i] == 'e' || arr[i] == 'E'){
                //如果前面有e/或者没有数字就返回false
                if(isEe||!isNum) return false;
                isEe = true;
                //有e/E后要把isNum设为false
                isNum = false;
            }else if(arr[i] == '+' ||arr[i] == '-'){
                //如果+-不在第一位而且前一位不是e/E就直接返回false
                if(i != 0 && arr[i-1] != 'e' && arr[i-1] != 'E') return false;
            }else return false;
        }
        return isNum;
    }
}
